package com.secondary.action.goodsAction;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.secondary.action.BaseAction;
import com.secondary.pojos.Category;
import com.secondary.pojos.Goods;
import com.secondary.pojos.GoodsPic;
import com.secondary.pojos.User;
import com.secondary.service.CategoryService;
import com.secondary.service.GoodsService;
import com.secondary.util.MyImageUtil;
@ParentPackage(value = "authority")
@Namespace(value = "/changeGoods")
@Action(value="changeGoods")
@Results({ 
	@Result(name = "gotoSave", location = "/user/saveGood.jsp"),
	@Result(name = "showUserGoods", location = "/user/updateGoods.jsp"),
	@Result(name = "deleteSuccess", location = "/changeGoods/changeGoods!showUserGoods.action",type = "redirect"),
	@Result(name = "findGood", location = "/user/updateGoodInfo.jsp"),
	@Result(name = "updateGoodsInfoSuccess", location = "/changeGoods/changeGoods!showUserGoods.action",type = "redirect"),
		})
public class ChangeGoodAction extends BaseAction{
	@Resource
	private GoodsService goodsService;
	@Resource
	private CategoryService categoryService;
	private List<Goods> userGoods;
	private String[] selectedGoods;
	private Goods good;
	private Goods goods;
	private List<Category> parentCategoryList;
	private List<Category> subCategoryList;
	private List<Category> category2;
	private File[] files;
	private String[] filesFileName;
	private String[] filescontentType;
	public String showUserGoods(){
		int userId=((User)super.getSessionAttribute("user")).getId();
		
		userGoods=goodsService.findGoodsByUserId(userId);
		
		return "showUserGoods";
	}

	public String gotoSaveGoods() {
		parentCategoryList = categoryService.findParentCategory(0);
		subCategoryList = categoryService.findParentCategory(parentCategoryList
				.get(0).getId());
		return "gotoSave";
	}

	public String getCategory2() {
		int id = Integer.parseInt(super.getRequestParameter("id"));
		category2 = categoryService.findParentCategory(id);
		JSONArray json = JSONArray.fromObject(category2);
		super.renderText(json.toString());
		
		return null;
	}
	public String deleteGoods(){
		
		List<Integer> idList=parseList(selectedGoods);
		goodsService.deleteGoods(idList);
		return "deleteSuccess";
	}
	public List<Integer> parseList(String[] ids){
		List<Integer> idList=new ArrayList<Integer>();
		for(int i=0;i<ids.length;i++){
			idList.add(Integer.parseInt(ids[i]));
		}
		
		return idList;
	}
	public String findGoodInfo() {
		int id = Integer.parseInt(super.getRequestParameter("id"));
		good = goodsService.findGood(id);
		return "findGood";

	}
	public String deletePic() {
		User user = (User) super.getSessionAttribute("user");
		String path = ServletActionContext.getServletContext().getRealPath(
				"/user_images_" + user.getId());
		int id = Integer.parseInt(super.getRequestParameter("id"));
		String name=super.getRequestParameter("name");
		MyImageUtil.deletePic(path,name,id,user.getId());
		goodsService.deletePic(id);
		return null;
	}

	public String updateGoodsInfo(){

		User user = (User) super.getSessionAttribute("user");
		String path = ServletActionContext.getServletContext().getRealPath(
				"/user_images_" + user.getId());
		MyImageUtil.savePics(files, path, filesFileName);
		if(filesFileName!=null){
		for (int i = 0; i < filesFileName.length; i++) {
			GoodsPic pic = new GoodsPic();
			pic.setPicUrl(filesFileName[i]);
			pic.setGoods(goods);
			goodsService.savePic(pic);
		}
		}
		System.out.println("____________________________");
		goodsService.updateGoodsInfo(goods);
		return "updateGoodsInfoSuccess";
	}

	
	public String updateGoodInfo(){
		return null;
	}
	public GoodsService getGoodsService() {
		return goodsService;
	}
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	public List<Goods> getUserGoods() {
		return userGoods;
	}
	public void setUserGoods(List<Goods> userGoods) {
		this.userGoods = userGoods;
	}
	public String[] getSelectedGoods() {
		return selectedGoods;
	}
	public void setSelectedGoods(String[] selectedGoods) {
		this.selectedGoods = selectedGoods;
	}
	public Goods getGood() {
		return good;
	}
	public void setGood(Goods good) {
		this.good = good;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<Category> getParentCategoryList() {
		return parentCategoryList;
	}

	public void setParentCategoryList(List<Category> parentCategoryList) {
		this.parentCategoryList = parentCategoryList;
	}

	public List<Category> getSubCategoryList() {
		return subCategoryList;
	}

	public void setSubCategoryList(List<Category> subCategoryList) {
		this.subCategoryList = subCategoryList;
	}

	public void setCategory2(List<Category> category2) {
		this.category2 = category2;
	}

	public File[] getFiles() {
		return files;
	}

	public void setFiles(File[] files) {
		this.files = files;
	}

	public String[] getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}

	public String[] getFilescontentType() {
		return filescontentType;
	}

	public void setFilescontentType(String[] filescontentType) {
		this.filescontentType = filescontentType;
	}
	
}
